package com.esms.struts.service;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

import com.esms.bean.Person;

public class ChartHelper {
	public static ChartHelper $ = new ChartHelper();
	private ChartHelper() {}	
	
	public JFreeChart appraise(List persons) {
		double[][] data = new double[3][persons.size()];
		String[] rowKeys = {"enterprise", "employee", "service"};
		String[] columnKeys = new String[persons.size()];
		for(int i = 0; i < persons.size(); i++) {
			Person person = (Person) persons.get(i);
			Map eMap = EnterpriseService.$.getAllEnterprises(person.getUserid());
			//c_sum
			//e_sum
			//s_sum
			List cList = EnterpriseService.$.getEnterpriseList(eMap);
			List eList = EnterpriseService.$.getEmployeeList(eMap);
			List sList = EnterpriseService.$.getServiceList(eMap);
			int c_sum = cList.size();
			int e_sum = eList.size();
			int s_sum = sList.size();
			
			//if 0 set 0.1
			data[0][i] = c_sum == 0 ? 0.1 : c_sum;
			data[1][i] = c_sum == 0 ? 0.1 : e_sum;
			data[2][i] = c_sum == 0 ? 0.1 : s_sum;
			columnKeys[i] = person.getUserid();
		}
		
		CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
		JFreeChart chart = ChartFactory.createBarChart3D("Appraise system","","",dataset,PlotOrientation.VERTICAL,true,false,false);
		chart.setBackgroundPaint(Color.white);
		CategoryPlot plot = chart.getCategoryPlot();
		CategoryAxis domainAxis = plot.getDomainAxis();
		plot.setDomainAxis(domainAxis);
		ValueAxis rangeAxis = plot.getRangeAxis();
		//设置最高的一个 Item 与图片顶端的距离
		rangeAxis.setUpperMargin(0.15);
		//设置最低的一个 Item 与图片底端的距离
		rangeAxis.setLowerMargin(0.15);
		plot.setRangeAxis(rangeAxis);
		BarRenderer3D renderer = new BarRenderer3D();
		renderer.setBaseOutlinePaint(Color.blue);
		//设置 Wall 的颜色
		renderer.setWallPaint(Color.gray);
		//设置每种柱的颜色
		renderer.setSeriesPaint(0, new Color(0, 0, 255));
		renderer.setSeriesPaint(1, new Color(0, 100, 255));
		renderer.setSeriesPaint(2, Color.GREEN);
		//设置每个person所包含的平行柱的之间距离
		renderer.setItemMargin(0.1);
		//显示每个柱的数值，并修改该数值的字体属性
		renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setItemLabelsVisible(true);
		plot.setRenderer(renderer);
		//设置柱的透明度
		plot.setForegroundAlpha(1);
		//设置显示位置
		plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
		plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
		
		return chart;
	}
}
